package com.mooc.ppjoke.utils;

import android.content.ComponentName;

import androidx.fragment.app.FragmentActivity;
import androidx.navigation.ActivityNavigator;
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavGraphNavigator;
import androidx.navigation.NavigatorProvider;
import androidx.navigation.fragment.FragmentNavigator;
import com.mooc.ppjoke.model.Destination;
import com.mooc.ppjoke.navigator.FixFragmentNavigator;
import com.wfc.libcommon.AppGlobals;

import java.util.HashMap;
public class NavGraphBuilder {

    public static void build(NavController controller, FragmentActivity activity,int containerId){
        NavigatorProvider provider=controller.getNavigatorProvider();
        //NavGraphNavigator 也是页面路由导航器的一种，只不过他比较特殊。
        // 它只为默认的展示页提供导航服务，但真正的跳转还是交给对应的navigator来完成的。
        NavGraph navGraph=new NavGraph(new NavGraphNavigator(provider));

        FixFragmentNavigator fragmentNavigator=new FixFragmentNavigator(activity,activity.getSupportFragmentManager(),containerId);
        provider.addNavigator(fragmentNavigator);
      //  FragmentNavigator fragmentNavigator=provider.getNavigator(FragmentNavigator.class);
        ActivityNavigator activityNavigator=provider.getNavigator(ActivityNavigator.class);
        HashMap<String, Destination> desConfig=AppConfig.getDestConfig();
        for(Destination  value:desConfig.values()){
            if(value.isFragment){
               FragmentNavigator.Destination destination=fragmentNavigator.createDestination();
               destination.setClassName(value.className);
               destination.setId(value.id);
               destination.addDeepLink(value.pageUrl);
               navGraph.addDestination(destination);
            }else{
                ActivityNavigator.Destination destination=activityNavigator.createDestination();
                //destination.setClassName(value.className);
                destination.setId(value.id);
                destination.addDeepLink(value.pageUrl);
                destination.setComponentName(new ComponentName(AppGlobals.getApplication().getPackageName(),value.className));
                navGraph.addDestination(destination);
            }
            if(value.asStarter){
                navGraph.setStartDestination(value.id);
            }
        }
        controller.setGraph(navGraph);
    }

}
